<!DOCTYPE html>
<html class="x-admin-sm">

<head>
    <meta charset="UTF-8">
    <title>编辑用户信息</title>
    <meta name="编辑用户信息" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport"
          content="width=device-width,user-scalable=yes, minimum-scale=0.4, initial-scale=0.8,target-densitydpi=low-dpi"/>
    <link rel="stylesheet" href="../../css/font.css">
    <link rel="stylesheet" href="../../css/xadmin.css">
    <script type="text/javascript" src="../../lib/layui/layui.js" charset="utf-8"></script>
    <script type="text/javascript" src="../../js/xadmin.js"></script>

    <!-- 引入公用JavaScript -->
    <script src="../../js/jquery.min.js"></script>
    <script src="../../js/app.js"></script>

    <!-- my JavaScript -->
    <script type="text/javascript" src="./user-data.js"></script>
</head>

<body>
<div class="layui-fluid">
    <div class="layui-row">
        <form class="layui-form">
            <div class="layui-form-item" style="display: none">
                <label for="userId" class="layui-form-label">
                    <span class="x-red">*</span>id</label>
                <div class="layui-input-inline">
                    <input type="text" id="userId" name="userId" required="" lay-verify="userId" autocomplete="off"
                           class="layui-input" disabled></div>
            </div>
            <div class="layui-form-item">
                <label for="workId" class="layui-form-label">
                    <span class="x-red">*</span>工号</label>
                <div class="layui-input-inline">
                    <input type="text" id="workId" name="workId" required="" lay-verify="workId" autocomplete="off"
                           class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label for="username" class="layui-form-label">
                    <span class="x-red">*</span>姓名</label>
                <div class="layui-input-inline">
                    <input type="text" id="username" name="username" required="" lay-verify="nikename"
                           autocomplete="off" class="layui-input"></div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">
                    <span class="x-red">*</span>性别</label>
                <div class="layui-input-inline">
                    男 <input type="radio" name="sex" value="1" class="layui-input">
                    女 <input type="radio" name="sex" value="0" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label for="age" class="layui-form-label">
                    <span class="x-red">*</span>年龄</label>
                <div class="layui-input-inline">
                    <input type="text" id="age" name="age" required="" lay-verify="age" autocomplete="off"
                           class="layui-input" oninput="value=value.replace(/[^\d]/g,'')"></div>
            </div>
            <div class="layui-form-item">
                <label for="roleId" class="layui-form-label">
                    <span class="x-red">*</span>用户归属</label>
                <div class="layui-input-inline">
                    <select name="roleId" id="roleId" disabled>
                        <!-- 用户归属列表数据区域 -->
                    </select>
                </div>
            </div>

            <div class="layui-form-item">
                <label class="layui-form-label"></label>
                <button type="button" class="layui-btn" lay-filter="save" lay-submit="save">保存</button>
            </div>
        </form>
    </div>
</div>
<script>
    /* 父页面获取传过来的id */
    var thisURL = document.URL;
    var getval = thisURL.split('?')[1];
    var workId = getval.split("=")[1].split("&")[0];
    var managerId = app.getManagerId()


    /* 从data.js中获取用户归属列表数据区域 */
    $.ajax({
        url: app.ServerUrl + 'v1/role/getSubRolesByManagerId?managerId=' + managerId,
        type: 'get',
        dataType: 'json',
        async: false,
        data: {},
        headers: {
            "Authorization": "460bcdb0-b326-4d6d-ac99-232c83ba6d80"
        },
        success: function (responseData) {
            // console.log(responseData);
            var result = responseData.objs[0];
            var opitonItems = "";

            // for (i = 0; i < result.length; i++) {
            //
            //     if (result[i].roleId === roleId) {
            //         opitonItems += '<option selected value="' + result[i].roleId + '">' + result[i]
            //             .role + '</option>';
            //     } else if (result[i].roleList[0].roleId === roleId) {
            //         opitonItems += '<option selected value="' + result[i].roleList[0].roleId + '">' +
            //             result[i].roleList[
            //                 0].role + '</option>';
            //     } else {
            //         opitonItems += '<option value="' + result[i].roleId + '">' + result[i].role +
            //             '</option>';
            //         opitonItems += '<option value="' + result[i].roleList[0].roleId + '">' + result[i]
            //             .roleList[
            //             0].role + '</option>';
            //     }
            // }
            // var roleId = $('#roleId');
            // roleId.append(opitonItems);
        },
        error: function () {
            layer.msg('请求失败!', {
                icon: 5
            })
        }
    })


    $.ajax({
        url: app.ServerUrl + "v1/user/queryUser",
        dataType: "json",
        type: "POST",
        async: false,
        data: {
            workId: workId,
            managerId: managerId
        },
        headers: {
            "Authorization": "460bcdb0-b326-4d6d-ac99-232c83ba6d80"
        },
        success: function (responseData) {
            console.log(responseData);
            var result = responseData.objs[0][0];
            var username = result.username;
            var age = result.age;
            var sex = parseInt(result.sex);
            var userId = result.userId
            var roleId = $('#roleId');


            // 表格中的默认数据渲染
            $("#workId").val(workId);
            $("#userId").val(userId);
            $("#username").val(username);
            $("#age").val(age);
            $("input[name='sex'][value='0']").attr("checked", sex == 0 ? true : false);
            $("input[name='sex'][value='1']").attr("checked", sex == 1 ? true : false);
            var opitonItems = '<option selected value="' + result.roleId + '">' + result
                .role + '</option>';
            roleId.append(opitonItems);

        }
    })


    /* 将修改后的数据提交至后台 */
    layui.use(['form', 'layer'],
        function () {
            $ = layui.jquery;
            var form = layui.form,
                layer = layui.layer;

            //自定义验证规则
            form.verify({
                nikename: function (value) {
                    if (value.length < 2) {
                        return '姓名至少得2个字符啊';
                    }
                },
                pass: [/(.+){2,12}$/, '密码必须2到12位'],
                repass: function (value) {
                    if ($('#L_pass').val() != $('#L_repass').val()) {
                        return '两次密码不一致';
                    }
                }
            });

            //监听提交
            form.on('submit(save)',
                function (data) {
                    console.log(data.field)
                    // 编辑用户Ajax
                    $.ajax({
                        url: app.ServerUrl + 'v1/user/modify?managerId=' + managerId,
                        type: 'post',
                        dataType: 'json',
                        async: false,
                        data: data.field,
                        success: function (data) {
                            if (data.resultStatus == 1) {
                                layer.msg('修改成功', {
                                    icon: 1,
                                    time: 1000
                                })
                            } else {
                                layer.msg(data.resultIns, {
                                    icon: 5,
                                    time: 1000
                                })
                            }
                        },
                        error: function (err) {
                            layer.msg('修改失败', {
                                icon: 5,
                                time: 1000
                            })
                        }
                    });

                });

        });
</script>
<script>
    var _hmt = _hmt || [];
    (function () {
        var hm = document.createElement("script");
        hm.src = "https://hm.baidu.com/hm.js?b393d153aeb26b46e9431fabaf0f6190";
        var s = document.getElementsByTagName("script")[0];
        s.parentNode.insertBefore(hm, s);
    })();
</script>

</body>

</html>